#ifndef BOOKUNRAR_H
#define BOOKUNRAR_H

#include <QObject>
#include <QProcess>
#include "bookitemlist.h"
#include "bookunrarqueuethread.h"

class Book;

class BookUnrar : public QObject
{
    Q_OBJECT

public:
    BookUnrar(QObject* parent = 0);

    void run();

    enum States {
        Nothing,
        Extract,
        ExtractFile,
        PageCount,
        FirstPage
    };

signals:
    void openedZipPage(int count, int total, QString text);
    void extractFinished(BookItemList::ErrorCodes code);
    void pageCount(int count);
    void firstImageExtracted(BookItemList::ErrorCodes code, Book* book);

public:
    void setArchiveName(QString text);
    void getPageCount();
    void getFirstImage(Book* book);
    void extractAll();

public slots:
    void startExtractFile(Book* book);
    void startFirstImage(Book* book);

private slots:
    void onError(QProcess::ProcessError error );
    void onFinished( int exitCode, QProcess::ExitStatus exitStatus );
    void onReadyReadStandardError();
    void onReadyReadStandardOutput();

private:
    void extractFile(Book* book);

private:
    QString     m_archiveName;
    bool        m_cancel;
    QProcess*   m_process;
    States      m_state;
    QString     m_firstPage;
    int         m_pageCount;
    int         m_files;
    QStringList m_params;
    BookUnrarQueueThread* m_queueExtractFile;
    BookUnrarQueueThread* m_queueFirstPage;
    Book*       m_currentBook;
};

#endif // BOOKUNRAR_H
